// 初始化导航栏
function initNavbar() {
    // 从localStorage获取玩家信息
    const player = JSON.parse(localStorage.getItem('player'));
    if (player) {
        $('#playerName').text(player.username);
    }

    // 退出按钮事件
    $('#logoutBtn').click(function() {
        localStorage.removeItem('player');
        MessageUI.showMessage('已成功退出登录', 'success');
        setTimeout(() => {
            window.location.href = '/login';
        }, 1000);
    });

    // 获取好友请求数量
    if (player) {
        $.ajax({
            url: `/api/friends/pending/${player.id}/count`,
            method: 'GET',
            success: function(count) {
                if (count > 0) {
                    $('#friendRequestCount').text(count);
                    $('#friendRequestCount').show();
                } else {
                    $('#friendRequestCount').hide();
                }
            },
            error: function(error) {
                console.error('Failed to get friend request count:', error);
                $('#friendRequestCount').hide();
            }
        });
    }
}

// 页面加载完成后初始化导航栏
$(document).ready(function() {
    initNavbar();
}); 